﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace Clinic.View.ViewModels
{
    public partial class PageViewModel<TItem> : ObservableValidator
    {
        /// <summary>
        /// 查询条件
        /// </summary>
        [ObservableProperty]
        private string serachTxt = string.Empty;
        /// <summary>
        /// 当前页码（从1开始）
        /// </summary>
        [ObservableProperty]
        private int pageIndex = 1;
        /// <summary>
        /// 跳转页码
        /// </summary>
        [ObservableProperty]
        private int jumpIndex;
        /// <summary>
        /// 每页数据条数
        /// </summary>
        [ObservableProperty]
        private int pageSize = 15;
        /// <summary>
        /// 总页数
        /// </summary>
        [ObservableProperty]
        private int pageCount;
        /// <summary>
        /// 数据总条数
        /// </summary>
        [ObservableProperty]
        private int totalCount;
        /// <summary>
        /// 数据集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<TItem> itemList;

        /// <summary>
        /// 页码改变命令
        /// </summary>
        public AsyncRelayCommand<FunctionEventArgs<int>> PageUpdatedCmd => new(JumpPage);

        public PageViewModel()
        {
            itemList = new ObservableCollection<TItem>();
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public virtual Task GetPageData()
        {
            if ((TotalCount / PageSize) % PageSize == 0)
            {
                PageCount = (TotalCount / PageSize);
                if (PageCount == 0)
                    PageCount = 1;
            }
            else
            {
                PageCount = (TotalCount / PageSize) + 1;
            }
            return Task.CompletedTask;
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public virtual Task PreviousPage()
        {
            if (PageIndex == 1)
            {
                return Task.CompletedTask;
            }
            PageIndex--;
            GetPageData();
            return Task.CompletedTask;
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <returns></returns>
        public virtual Task NextPage()
        {
            if (PageIndex == PageCount)
            {
                return Task.CompletedTask;
            }
            PageIndex++;
            GetPageData();
            return Task.CompletedTask;
        }

        /// <summary>
        /// 跳转页码
        /// </summary>
        /// <returns></returns>
        public virtual Task JumpPage(FunctionEventArgs<int> info)
        {
            JumpIndex = info.Info;
            if (JumpIndex > PageCount || JumpIndex < 1)
            {
                return Task.CompletedTask;
            }
            PageIndex = JumpIndex;
            GetPageData();
            return Task.CompletedTask;
        }

        /// <summary>
        /// 加载操作权限（按钮权限）
        /// </summary>
        /// <returns></returns>
        public virtual Task LoadPlayPermission()
        {
            return Task.CompletedTask;
        }
    }
}
